﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CodeBuilder2.View.CB_UserControl.Content;
using System.Threading;
using System.Windows.Threading;
using CodeBuilder2.Model.Constant;

namespace CodeBuilder2.View.CB_UserControl.Panel
{
    /// <summary>
    /// Interaction logic for RichPanel.xaml
    /// </summary>
    public partial class RichPanel : UserControl
    {
        public string PanelName { get; set; }
        public RichPanel()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            // 在主显示区显示正在加载字样
            TextBlock tbk = new TextBlock();
            tbk.Text = UIConstant.LOADING_CONTENT;
            grdContentPane.Children.Add(tbk);
            tbkPanelName.Text = PanelName;

            #region 开启独立线程完成内容加载

            Thread thread = new Thread(new ThreadStart(this.InitPanel));
            thread.Start();

            #endregion

            //Thread thread2 = new Thread(new ThreadStart(this.AutoHide));
            //thread2.Start();
        }

        void InitPanel()
        {
            // Thread.Sleep(5000); 模拟费时操作
            ThreadStart start = () => LoadContent();

            // 当执行到这里，开始调用 start 委托时，就已经回到主线程（UI 线程）了，界面将暂停响应用户
            // 输入事件，包括但不限于：鼠标移动、点击等
            Dispatcher.BeginInvoke(start, DispatcherPriority.Normal);
        }

        private void LoadContent()
        {
            switch (PanelName)
            {
                case "数据库":
                    DataBaseContent dbc = new DataBaseContent();
                    //Thread.Sleep(1000);
                    // 清空在加载开始前表格控件中已经存在的元素
                    grdContentPane.Children.Clear();
                    grdContentPane.Children.Add(dbc);
                    break;
                case "资源管理器":
                    break;
                default:
                    break;
            }
        }

        void AutoHide()
        {
            bool flag = true;
            while (flag)
            {
                if (this.IsMouseOver)
                {
                    Thread.Sleep(1000);
                }
                else
                {
                    ThreadStart start = delegate()
                    {
                        (this.Parent as DockPanel).Children.Clear();
                    };
                    this.Dispatcher.BeginInvoke(start);
                }
            }
        }
    }
}
